<?php

require "../types.php";
require "../utility.php";

http_response_code(403);

if (check_keys($_GET, "bookID")) {
    $book_id = $_GET["bookID"];
    http_response_code(200);
    
    $conn = connect_mysql();

    $query_str = "SELECT comments.ID, userName, content, score 
                  FROM Comments, user WHERE bookID = '$book_id' AND userID=user.ID ORDER BY comments.ID DESC;";
    $query = $conn->query($query_str);

    $comments = [];
    while ($comment_data = $query->fetch_row()) {
        $comment = Comment::fromArray($comment_data);
        $comments[] = $comment;
    }

    $query->close();
    $conn->close();

    echo json_encode($comments, JSON_UNESCAPED_UNICODE);
}

?>